package 网络编程项目.login.utiles;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//利用单例模式创建连接
public class ConnectSingleton {
  private static volatile Connection connection = null;

  private ConnectSingleton() {
  }

  public static Connection getConnection() {
    if (connection == null) {
      synchronized (ConnectSingleton.class){
        if(connection==null){
          try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            connection = DriverManager.getConnection("jdbc:mysql:///qq?serverTimezone=Asia/Shanghai", "root", "root");
          } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
          }
        }
      }
    }
    return connection;
  }
}
